package org.jeecg.modules.front.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.bank.entity.Bank;
import org.jeecg.modules.bank.entity.YcUserhomeBank;
import org.jeecg.modules.bank.service.IBankService;
import org.jeecg.modules.bank.service.IYcUserhomeBankService;
import org.jeecg.modules.front.entity.vo.AuthenticationUserhome;
import org.jeecg.modules.front.entity.vo.FrontUnbindBank;
import org.jeecg.modules.front.entity.vo.FrontUserhomeBank;
import org.jeecg.modules.front.util.YinShengUtil;
import org.jeecg.modules.gongqiang.entity.Userhome;
import org.jeecg.modules.gongqiang.entity.UserhomeAuthentication;
import org.jeecg.modules.gongqiang.service.IUserhomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author scott
 * @since 2018-12-17
 */
@RestController
@RequestMapping("/front/bank")
@Api(tags = "前台银行卡模块")
@Slf4j
public class FrontBankController {
    @Autowired
    private IBankService bankService;

    @Autowired
    private IUserhomeService userhomeService;

    @Autowired
    private IYcUserhomeBankService ycUserhomeBankService;



    @ApiOperation("查询网银支持银行信息")
    @RequestMapping(value = "/getBank", method = RequestMethod.GET)
    public Result<List<Bank>> getBank(@ApiParam(name = "protocolType",value = "协议类型" +
            "01-协议支付" +
            "03-个人网银支付" +
            "04-企业网银支付",required = true) String protocolType) {

        Result<JSONObject> result=YinShengUtil.QueryBankList(protocolType);
        JSONObject yingshengResult=result.getResult();
        JSONArray jsonArray=yingshengResult.getJSONArray("bankList");
        List<Bank> bankList=new ArrayList<>();
        List<Bank> list=bankService.list();

        for (int i = 0; i < jsonArray.size(); i++) {
            Bank bank=new Bank();
            // 获取当前索引的JSON对象
            JSONObject jsonObject = jsonArray.getJSONObject(i);

           for(Bank temp:list)
           {
               if(temp.getBankName().equals(jsonObject.get("bankName")))
               {
                   bank.setBankName(temp.getBankName());
                   bank.setBankCode(temp.getBankCode());
                   bank.setId(temp.getId());
                   bank.setIconUrl(temp.getIconUrl());
                   bank.setIsMobile3(temp.getIsMobile3());
                   bank.setCode(jsonObject.getString("code"));
                   bankList.add(bank);
                   break;
               }
           }

        }

        return Result.OK(bankList);
    }


    @ApiOperation("查询绑定支持银行信息")
    @RequestMapping(value = "/bankList", method = RequestMethod.GET)
    public Result<List<Bank>> getBank() {
        List<Bank> list=bankService.list();
        return Result.OK(list);
    }

    @ApiOperation("绑定银行卡")
    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    public Result<String> bind(@RequestBody FrontUserhomeBank frontUserhomeBank, HttpServletRequest req) {
        Integer userId = JwtUtil.getUserId(req.getHeader("X-Access-Token"));
        Userhome userhome = userhomeService.getById(userId);
        QueryWrapper<YcUserhomeBank> ycUserhomeBankQueryWrapper =new QueryWrapper<>();
        ycUserhomeBankQueryWrapper.eq("userhome_id",userId);
        ycUserhomeBankQueryWrapper.eq("bank_number",frontUserhomeBank.getBankNumber());
        YcUserhomeBank ycUserhomeBank =ycUserhomeBankService.getOne(ycUserhomeBankQueryWrapper);
        if(oConvertUtils.isNotEmpty(ycUserhomeBank))
        {
            return Result.error("该银行卡号已绑定");
        }
        return ycUserhomeBankService.bind(frontUserhomeBank,userhome.getId());
    }


    @ApiOperation("解绑银行卡")
    @RequestMapping(value = "/unbind", method = RequestMethod.POST)
    public Result<String> bind(@RequestBody FrontUnbindBank frontUnbindBank, HttpServletRequest req) {
        Integer userId = JwtUtil.getUserId(req.getHeader("X-Access-Token"));
        Userhome userhome = userhomeService.getById(userId);
        return ycUserhomeBankService.unbind(frontUnbindBank.getId(),userhome.getId());
    }

    @ApiOperation("查询用户银行卡")
    @RequestMapping(value = "/findBankByUserhomeId", method = RequestMethod.GET)
    public Result<List<YcUserhomeBank>> findBankByUserhomeId( HttpServletRequest req) {
        Integer userId = JwtUtil.getUserId(req.getHeader("X-Access-Token"));
        QueryWrapper<YcUserhomeBank> ycUserhomeBank=new QueryWrapper<>();
        ycUserhomeBank.eq("userhome_id",userId);
        List<YcUserhomeBank> ycUserhomeBankList=ycUserhomeBankService.list(ycUserhomeBank);
        List<Bank> bankList=bankService.list();
        for (YcUserhomeBank temp:ycUserhomeBankList)
        {
            for (Bank temp2:bankList)
            {
                if(temp.getBankTypeId().equals(temp2.getId()))
                {
                    temp.setIconUrl(temp2.getIconUrl());
                    break;
                }
            }
        }
        return Result.OK(ycUserhomeBankList);
    }
}